package com.sunop.userservice.util;

import cn.dev33.satoken.stp.StpInterface;
import com.sunop.userservice.mapper.UserMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * 自定义权限认证接口扩展，Sa-Token 将从此实现类获取每个账号拥有的权限码
 */
@Component
@RequiredArgsConstructor
public class StpInterfaceImpl implements StpInterface {

	private final UserMapper userMapper;

	/**
	 * 返回一个账号所拥有的权限码集合 
	 */
	@Override
	public List<String> getPermissionList(Object loginId, String loginType) {
		// 本list仅做模拟，实际项目中要根据具体业务逻辑来查询权限
		List<String> list = new ArrayList<String>();
		list.add("101");
		list.add("user.add");
		list.add("user.update");
		list.add("user.get");
		// list.add("user.delete");
		list.add("art.*");
		return list;
	}

	/**
	 * 返回一个账号所拥有的角色标识集合 
	 */
	@Override
	public List<String> getRoleList(Object loginId, String loginType) {
		List<String> list = new ArrayList<>();
		if(loginId instanceof String) {
			loginId = Integer.parseInt((String) loginId);
		}
		int id = (int)loginId;
		switch (userMapper.selectRoleById(id)){
			case 0:
				list.add("student");break;
			case 1:
				list.add("teacher");break;
		}
		return list;
	}

}
